/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package functions;

import dto.Account;
import java.util.Properties;

import javax.mail.Message;
//import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 *
 * @author BLACK
 */
public class ServerMails {

    /**
     * username, password ... the mail & the password of the Server mail
     * that will be used to send mails.
     */
    final String username = "project.server33@gmail.com";
    final String password = "iti00000";
    Properties props;

    public ServerMails() {

        props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");

    }
    
    /**
     * Method used to send confirmation mails after user registration.
     */
    public Boolean sendMail(Account userAccount) {
        
        Boolean success=false;
        
        String confirmationMsg=" Welcome,"+userAccount.getFirstName()+" "+userAccount.getLastName()+"\n \n "
                + "Now you can use our chatting Service. \n that's your E-mail Address:"+ userAccount.getEmail()+"\n And that's your Password: "+userAccount.getPassword()+"\n Please, Don't Forget Them. \n Thanks.";
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });


        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("project.server33@gmail.com"));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(userAccount.getEmail()));
            
            message.setSubject("Testing Subject");
            message.setText(confirmationMsg);

            Transport.send(message);
            success=true;

        } catch (javax.mail.MessagingException ex) {
            ex.printStackTrace();
            // Logger.getLogger(SendMailTLS.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return success;
    }
    /**
     * method used to send mails from users through server mails.
     */
    public Boolean sendMail (String sender, String reciever,String title,String usermessage)
    {   
        Boolean success=false;
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });


        try {
            Message message = new MimeMessage(session);
            //message.setFrom(new InternetAddress("project.server33@gmail.com"));
            message.setFrom(new InternetAddress("project.server33@gmail.com"));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(reciever));
            
            message.setSubject(title);
            message.setText("This Message has sent you From: "+sender+"\n\n"+usermessage);

            Transport.send(message);
            success=true;

        } catch (javax.mail.MessagingException ex) {
            ex.printStackTrace();
            // Logger.getLogger(SendMailTLS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return success;
  
    }
    
}
